from config.configs import DINGSTOCK_HOST, DINGSTOCK_PORT, GOAT_ERP_HOST, GOAT_ERP_PORT, DESKTOP_PATH, DATA_PATH
import pymongo
from tools.funciton import read_xlsx_file
import os
import jionlp as jio
import requests
import random
import string

ding = pymongo.MongoClient(host=DINGSTOCK_HOST, port=DINGSTOCK_PORT, authMechanism='DEFAULT', authSource='admin',
                           directConnection=True)
express = ding.express


def generate_random_string(length):
    # 定义字母和数字的字符集
    characters = string.ascii_letters + string.digits
    # 使用random.choices从字符集中随机选择指定数量的字符
    # 注意：random.choices在Python 3.6+中可用
    random_string = ''.join(random.choices(characters, k=length))
    return random_string


def update_system_addresses():
    """更新递潮得物,退货仓收件地址信息"""
    url = 'https://source.showdoc.com.cn/server/index.php?s=/api/page/info'
    header = {"content-type": "application/x-www-form-urlencoded", "referer": "https://www.showdoc.com.cn/",
              "Host": "source.showdoc.com.cn", "Content-Length": "40",
              "Postman-Token": "4ef78c76-c076-419c-a354-6c1fd1ef9523",
              "Cookie": "PHPSESSID=4f1003940bad1b19855b60789bb85a4e; think_language=zh-CN"}
    param = {"page_id": "10523208948277353", "_item_pwd": "QYNB"}
    ids, names, mobiles, province, city, district, address, tags, num = [], [], [], [], [], [], [], [], 1
    res = requests.post(url=url, headers=header, data=param)
    addresses = res.json()['data']['page_content'].splitlines(keepends=False)
    for i in range(2, len(addresses)):
        s = addresses[i][1: -1].split('\t')
        ids.append(s[0])
        names.append(s[1][1: -1])
        mobiles.append(s[2][0: -1])
        province.append(jio.parse_location(s[3], town_village=False)['province'])
        city.append(jio.parse_location(s[3], town_village=False)['city'])
        district.append(jio.parse_location(s[3], town_village=False)['county'])
        address.append(jio.parse_location(s[3], town_village=False)['detail'])
        tags.append(jio.parse_location(s[3], town_village=False)['city'].split('市')[0] + '园区')
    express.addresses.delete_many({'type': 'dewu'})
    express.addresses.delete_many({"type": "brandreturn"})
    for z in range(len(ids)):
        param = {
            "_id": generate_random_string(10),
            "category": "receive",
            "type": "dewu",
            "username": names[z],
            "mobile": mobiles[z],
            "province": province[z],
            "city": city[z],
            "district": district[z],
            "address": address[z],
            "yisuAddressId": ids[z],
            "number": str(num).zfill(4),
            "isDefault": False,
            "createdAt": 1716430606000,
            "updatedAt": 1716430606000,
            "__v": 0,
            "tag": tags[z]
        }
        num += 1
        express.addresses.insert_one(param)
    file = os.path.join(DATA_PATH, 'brand_return_address.xlsx')
    names = read_xlsx_file(file, 1)
    address = read_xlsx_file(file, 2)
    tags = read_xlsx_file(file, 0)
    for i in range(len(names)):
        param = {
            "_id": generate_random_string(10),
            "category": "receive",
            "type": "brandreturn",
            "username": names[i].split(' ')[0],
            "mobile": names[i].split(' ')[1],
            "province": jio.parse_location(address[i], town_village=False)['province'],
            "city": jio.parse_location(address[i], town_village=False)['city'],
            "district": jio.parse_location(address[i], town_village=False)['county'],
            "address": jio.parse_location(address[i], town_village=False)['detail'],
            "number": str(num).zfill(4),
            "isDefault": False,
            "createdAt": 1716430606000,
            "updatedAt": 1716430606000,
            "__v": 0,
            "tag": tags[i]
        }
        num += 1
        express.addresses.insert_one(param)



print(express.balances.find_one({'userId': "tPgjmKGJTz"}))




